package com.app.megaprosoft.modyimcarcare;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import com.app.megaprosoft.DB.Company;
import com.app.megaprosoft.DB.FFUser;
import com.app.megaprosoft.DB.SQLiteDBClass;
import com.app.megaprosoft.DB.SQLiteDB_POS;
import com.app.megaprosoft.DB.ServicePOS;
import com.app.megaprosoft.DB.UserGroup;
import com.app.megaprosoft.general.DateTime;
import com.app.megaprosoft.modyimcarcare.Carcare_StatusChange.LoadingLogin;
import com.megaprosoft.modyimcarcare.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class Carcare_History extends Activity {
	
	public static final int 	DIALOG_DOWNLOAD_PROGRESS = 0;
	
//	AlertDialog alert;
	private Button  			_Button_Report,
								_Button_Search;
	
	private TextView 			_TextView_company,
								_TextView_date;
	private EditText 			_EditText_Search;
	
	private String   			FFUserOID
								,UserName
								,DepCode
								,DepName
								,UserGroupOID
								,UserGroupName
								,PicPath
								,UserGroupCode="A"
								,ListService[][]
								,TabletCode;
	
	private ListView 			Carcare_ServiceListView;
	
	//Intent=
	
	private CarCare_Adapter_ListService carcare_listservice_adapter;
	
	UserGroup usergroup		 	= new UserGroup();
	ServicePOS servicepos 		= new ServicePOS();
	FFUser ffuser 				= new FFUser();
	Carcare_SessionManagement	session;
	SQLiteDB_POS 				sqlite_pos;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.a_carcare_activity_report);
		
		
		servicepos.setContext(getApplicationContext());
		ffuser.setContext(getApplicationContext());
		usergroup.setContext(getApplicationContext());
		
		sqlite_pos = new SQLiteDB_POS(this);
		sqlite_pos.getWritableDatabase();
    	
		_EditText_Search    = (EditText) findViewById(R.id.edittext_search);
		final InputMethodManager imm = (InputMethodManager)getSystemService(
			      Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(_EditText_Search.getWindowToken(), 0);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		_TextView_company 	= (TextView) findViewById(R.id.a_main_company);
		_TextView_date 		= (TextView) findViewById(R.id.a_main_date);
		
		session = new Carcare_SessionManagement(getApplicationContext());
		session.checkLogin();
        HashMap<String, String> user = session.getUserDetails();

        FFUserOID = user.get(Carcare_SessionManagement.KEY_FFUserOID);
        UserName = user.get(Carcare_SessionManagement.KEY_UserName);
        DepCode = user.get(Carcare_SessionManagement.KEY_DepCode);
        DepName = user.get(Carcare_SessionManagement.KEY_DepName);
        UserGroupOID = user.get(Carcare_SessionManagement.KEY_UserGroupOID);
        UserGroupName = user.get(Carcare_SessionManagement.KEY_UserGroupName);
        UserGroupCode = user.get(Carcare_SessionManagement.KEY_UserGroupCode);
        PicPath = user.get(Carcare_SessionManagement.KEY_PicPath);
        TabletCode = user.get(Carcare_SessionManagement.KEY_TabletCode);
        
        _TextView_company.setText(UserGroupName);
				
//////////////////////////////////////// Button /////////////////////////////		
      
        _Button_Search = (Button) findViewById(R.id.search);
        _Button_Search.setOnClickListener(buttonSearch);
		
		Calendar mCalendar = Calendar.getInstance();
		
		_TextView_date.setText(getFormat().format(mCalendar.getTime()));
		
		Carcare_ServiceListView = (ListView) findViewById(R.id.carcare_service_listservice);
		
		Define_ListService(UserGroupCode);
		
	}
	public SimpleDateFormat getFormat(){
//	    return new SimpleDateFormat("MMM, yyyy");
		return new java.text.SimpleDateFormat("dd MMMM yyyy");
	}; 

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Define_ListService(UserGroupCode);
		servicepos.setContext(getApplicationContext());
		ffuser.setContext(getApplicationContext());
		usergroup.setContext(getApplicationContext());
	}
	
	public void Define_ListService(String StrCar){	
		
		carcare_listservice_adapter = new CarCare_Adapter_ListService(Carcare_History.this, 
										R.layout.list_service, new ArrayList<CarCare_ListService>());
		Carcare_ServiceListView.setAdapter(carcare_listservice_adapter);
		int length = 0;
		try{
			ListService = servicepos.SelectReport("",UserGroupCode);
			length = ListService.length;
		
		}catch(Exception e){
			
		}
		int Service=0;
		int check=0;
		for(int i=0;i<length;i++){
			try{
				String _idcar;
				if(ListService[i][2].equals("")){
					_idcar="ไม่ระบุ";
				}else{
					_idcar=ListService[i][2].toString();
				}
				carcare_listservice_adapter.insert(new CarCare_ListService( length-i,
																			ListService[i][1].toString(),
																			_idcar, 
																			ListService[i][5].toString(),
																			ListService[i][9].toString(),
																			ListService[i][8].toString(),
																			ListService[i][19].toString()),
																			0);
//				
				if(ListService[i][8].toString().equals("รอปล่อย")){
					check=check+1;
				}else{
					Service=Service+1;
				}
			}catch(Exception e){
				
			}
        }
		
	
		
	}
	
	//----------------------Button Operation------------------------------//

    private OnClickListener buttonSearch = new OnClickListener() {
        public void onClick(View v) {
        	String textsearch = _EditText_Search.getText().toString();
        	if(!textsearch.equals("")){
        		Define_ListService(textsearch);
        	}
        }
    };
   
    @Override
	public void onBackPressed() {
	  
	        new AlertDialog.Builder(this)
	        .setTitle("กลับหน้าจอหลัก")
	        .setIcon(R.drawable.erroralert)
	        .setMessage("ยืนยันการกลับหน้าจอหลัก")
	                .setNegativeButton(android.R.string.yes, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                    	Intent newActivity = new Intent(Carcare_History.this,Carcare_Main.class);
	            			startActivity(newActivity);
	                        finish();
	                    }
	                })
	                .setPositiveButton(android.R.string.no, new DialogInterface.OnClickListener() {

	                    public void onClick(DialogInterface arg0, int arg1) {
	                    	
	                    }
	                }).create().show();
	}
						
}
